package neu.soft.service.impl;

import neu.soft.mapper.DistributionOrderMapper;
import neu.soft.service.DistributionOrderService;
import neu.soft.vo.PageBean;
import neu.soft.vo.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class DistributionOrderServiceImpl implements DistributionOrderService {
    @Autowired
    private DistributionOrderMapper distributionOrderMapper;

    /**
     * 生成一条分发单
     * @author zhh
     * @param map
     * @return
     */
    @Override
    public ResultBean<Integer> addDistributionOrder(Map<String,Object> map){
        int result = distributionOrderMapper.addDistributionOrder(map);
        return ResultBean.success();
    }

    /**
     * 展示所有的分发单和其对应的货物信息
     * @author zhh
     * @return
     */
    @Override
    public ResultBean<PageBean<Map<String,Object>>> showAllDistributionOrder(Map<String,Object> map){
        ResultBean<PageBean<List<Map<String,Object>>>> resultBean = new ResultBean<>();
        PageBean<Map<String,Object>> pageBean = new PageBean<>();
        int pageNow = (int) map.get("pageNow");
        int pageSize = (int) map.get("pageSize");
        List<Map<String,Object>> dList = distributionOrderMapper.showAllDistributionOrder(map);
        for(Map<String,Object> dMap:dList){
            Date date = (Date) dMap.get("distributeTime");
            if(date!=null){
                SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd");
                String s2 = sdf2.format(date);
                dMap.put("distributeTime",s2);
            }
        }
        Long distributionRows = distributionOrderMapper.getDistributionRows(map);
        if(dList == null){
            return ResultBean.error();
        }
        else {
            pageBean.setRowData(dList);
            pageBean.setTotalRows(distributionRows);
            pageBean.setTotalPages((int) (distributionRows % pageSize == 0 ? (distributionRows/pageSize) : (distributionRows/pageSize + 1)));
            return ResultBean.success(pageBean);
        }
    }
}
